import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:proj_sfv/component/SimpleVideo.dart';
import 'package:proj_sfv/component/VideoEditDialog.dart';
import 'package:proj_sfv/domain/Video.dart';
import 'package:proj_sfv/domain/videoVo.dart';
import 'package:proj_sfv/http/api.dart';
import 'package:proj_sfv/http/requests.dart';
import 'package:proj_sfv/store/MyArguments.dart';
import 'package:video_player/video_player.dart';

class MyVideoDetailPage extends StatefulWidget {
  VideoVo video;
  String token;
  bool _editable;
  VideoPageType type;
  var onDeleteVideo;
  var onUpdateVideo;
  String collectName;

  MyVideoDetailPage(this.video, this._editable, this.token, this.onDeleteVideo, this.onUpdateVideo, {VideoPageType type =
      VideoPageType.MY, String collectName}): this.type = type, this.collectName = collectName;

  @override
  _MyVideoDetailPage createState() => _MyVideoDetailPage();
}

class _MyVideoDetailPage extends State<MyVideoDetailPage> {
  VideoPlayerController _controller;
  Video video = Video.plain();

  @override
  void initState() {
    super.initState();
    var url = Uri.http(cosHost3, "${widget.video.videoId}.mp4").toString();
    _controller = VideoPlayerController.network(url);
    _controller.initialize().whenComplete(() {
      setState(() {
        _controller.play();
      });
    });
    getVideoDetailRequest(context, widget.token, widget.video.videoId).then((value) {
      setState(() {
        video = value;
      });
    });
  }


  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(video.videoName),
      ),
      body: Column(
        children: [
          SizedBox(
            height: 600,
            child: Center(
              child: Expanded(
                child: AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  video.videoName,
                  style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                ),
                SizedBox(height: 8),
                Text(video.description),
                SizedBox(height: 8),
                Row(
                  children: [
                    Text(
                      '点赞：${video.likeCount}',
                      style: TextStyle(fontSize: 16),
                    ),
                    SizedBox(width: 16),
                    IconButton(
                      icon: Icon(Icons.delete),
                      onPressed: widget.onDeleteVideo == null ? null : () {
                        if (widget.type == VideoPageType.MY) {
                          widget.onDeleteVideo(widget.video);
                        } else if (widget.type == VideoPageType.LIKE) {
                          widget.onDeleteVideo(widget.video, VideoPreference.LIKE);
                        } else if (widget.type == VideoPageType.DISLIKE) {
                          widget.onDeleteVideo(widget.video, VideoPreference.DISLIKE);
                        } else if (widget.type == VideoPageType.COMMON){
                          widget.onDeleteVideo(widget.video, widget.collectName);
                        }
                      },
                    ),
                    Visibility(
                      visible: widget._editable,
                      child: IconButton(
                        icon: Icon(Icons.edit),
                        onPressed: () async {
                           var result = await showDialog<Map<String, String>>(context: context, builder: (builder) => VideoEditDialog(
                            videoName: video.videoName,
                            videoDesc: video.description,
                            videoUrl: video.videoUrl,
                            videoCoverUrl: video.profileUrl,
                            ));
                           if (result != null) {
                             var resp = await updateVideoInfoRequest(context, widget.token, video.videoId, video.authorId,
                              videoName: result['videoName'],
                               description: result['videoDesc'],
                               tags: video.tags
                             );
                             if (resp != null) {
                               setState(() {
                                 video.description = result['videoDesc'];
                                 video.videoName = result['videoName'];
                                 widget.onUpdateVideo(video, widget.video);
                               });
                               Fluttertoast.showToast(msg: "修改成功");
                             }
                           }
                            // widget.onUpdateVideo(result);
                        },
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if (_controller.value.isPlaying) {
              _controller.pause();
            } else {
              _controller.play();
            }
          });
        },
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}
